import React from "react";
import {View} from "react-native";
import InputForm from "../components/form/InputForm";
import {globalStore} from "../libs/ShareStore";
import EChatIMApis from "../libs/sdk/echatim/core/apis/EChatIMApis";
import SwitchForm from "../components/form/SwitchForm";
import ANavigationComponent, {ANavigationComponentProp} from "../components/navigator/ANavigationComponent";
import EButton from "../components/utils/EButton";
import EHr from "../components/utils/EHr";
import {pageStyle} from "./style/pageStyle";
import AvatarHeader from "../components/utils/AvatarHeader";
import UserGetDTO from "../libs/sdk/echatim/model/dto/UserGetDTO";


type ModifyFriendInfoPageProp = {
    // friendAuid:string
} & ANavigationComponentProp

type ModifyUserInfoPageState = {
    avatar:string,
    name:string,
    online:number,
}


export default class ModifyFriendInfoPage extends ANavigationComponent<ModifyFriendInfoPageProp, ModifyUserInfoPageState>  {
    constructor(props:ModifyFriendInfoPageProp) {
        super(props);
        const userInfo = this.getNavigationParam('info') as UserGetDTO;
        this.state = {
            avatar:userInfo.avatar,
            name:userInfo.name,
            online:this.getUserOnlineStatus(userInfo.auid)
        };
    }

    private getUserOnlineStatus(auid:string):number{
        const targetFriend = globalStore.friendList.filter(value => {
            return value.auid === auid;
        });
        if(targetFriend.length > 0){
            return targetFriend[0].online;
        }
        const targetSession = globalStore.sessionList.filter(value => {
            return value.fromUser === auid;
        });
        if(targetSession.length > 0){
            return targetSession[0].online;
        }
        return 0;
    }

    delFriend(){
        EChatIMApis.delFriend.call(
            {
                auid:globalStore.userInfo.auid,
                targetAuid: globalStore.chatsContent.friendInfo.auid,
            }).then((resp) => {
            globalStore.showToast("操作成功");
            // app.reloadData("friend");
            globalStore.requestFriendUpdate();
        }).catch((e) => {
            globalStore.showApiErrorToast(e);
        })
    }

    modifyFriendBlackList(on:boolean) {
        var prevStatus = globalStore.chatsContent.friendInfo.blacklist;
        var toChange = prevStatus ? 'REMOVE_BLACKLIST' : 'ADD_BLACKLIST';
        EChatIMApis.modifyBlacklistForbid.call(
            {
                auid:globalStore.userInfo.auid,
                targetAuid: globalStore.chatsContent.friendInfo.auid,
                type:toChange
            }).then((resp) => {
            globalStore.chatsContent.friendInfo.blacklist = on;
            globalStore.showToast("修改成功");
        }).catch((e) => {
            globalStore.showApiErrorToast(e);
        })
    }

    modifyFriendForbid(on:boolean) {
        var prevStatus = globalStore.chatsContent.friendInfo.forbid;
        var toChange = prevStatus ? 'REMOVE_FORBID' : 'ADD_FORBID';
        EChatIMApis.modifyBlacklistForbid.call(
            {
                auid:globalStore.userInfo.auid,
                targetAuid: globalStore.chatsContent.friendInfo.auid,
                type:toChange
            }).then((resp) => {
            globalStore.chatsContent.friendInfo.forbid = on;
            globalStore.showToast("修改成功");
        }).catch((e) => {
            globalStore.showApiErrorToast(e);
        })
    }


    onModifyFriendInfo():void{
        globalStore.onModifyFriendInfoPressOk();
    }

    render() {
        return (
            <View style={pageStyle.container}>
                <AvatarHeader avatar={globalStore.userInfo.avatar}
                              headerFirstText={this.state.name}
                              headerSecondText={this.state.online === 0 ? '[离线]' : '[在线]'} />
                <InputForm startText={'备注:'} defaultValue={globalStore.chatsContent.friendInfo.alias} onChangeInput={(text)=>{globalStore.chatsContent.friendInfo.alias = text}} />
                <InputForm startText={'生日:'}  editable={false} defaultValue={globalStore.chatsContent.friendInfo.birth} onChangeInput={(text)=>{globalStore.chatsContent.friendInfo.birth = text}} />
                <InputForm startText={'手机:'}  editable={false} defaultValue={globalStore.chatsContent.friendInfo.mobile} onChangeInput={(text)=>{globalStore.chatsContent.friendInfo.mobile = text}} />
                <InputForm startText={'邮箱:'}  editable={false} defaultValue={globalStore.chatsContent.friendInfo.email} onChangeInput={(text)=>{globalStore.chatsContent.friendInfo.email = text}} />
                <InputForm startText={'签名:'}  editable={false} defaultValue={globalStore.chatsContent.friendInfo.sign} onChangeInput={(text)=>{globalStore.chatsContent.friendInfo.sign = text}} />
                <SwitchForm startText={'禁言:'}  defaultValue={globalStore.chatsContent.friendInfo.forbid} onChangeSwitch={(on)=>{this.modifyFriendBlackList(on)}} />
                <SwitchForm startText={'黑名单:'}  defaultValue={globalStore.chatsContent.friendInfo.blacklist} onChangeSwitch={(on)=>{this.modifyFriendForbid(on)}} />
                <EButton title={'修改好友信息'} onPress={()=>{this.onModifyFriendInfo()}}/>
                <EHr height={10}/>
                <EButton title={'删除好友'} onPress={()=>{this.delFriend()}}/>
            </View>
        )
     }

}
